//
//  ViewController.m
//  UI15_AFN
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 monster. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "EventsModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc
{
    [_array release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    
    UIButton *getButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [getButton setTitle:@"AFN get请求" forState:UIControlStateNormal];
    getButton.backgroundColor = [UIColor redColor];
    getButton.frame = CGRectMake(20, 100, 150, 40);
    [getButton addTarget:self action:@selector(afnGetRequest) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:getButton];
    
    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [postButton setTitle:@"AFN post请求" forState:UIControlStateNormal];
    postButton.backgroundColor = [UIColor redColor];
    postButton.frame = CGRectMake(200, 100, 150, 40);
    [postButton addTarget:self action:@selector(afnPostRequest) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:postButton];

    [self afnGetRequest];
    
    
}

-(void)afnGetRequest
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSDictionary *bigdic = responseObject;
        NSString *total = [bigdic objectForKey:@"total"];
        NSMutableArray *eventsArray = [bigdic objectForKey:@"events"];
        //遇到数组就遍历
        NSLog(@",,,,,,,%@", eventsArray);
        for (NSDictionary *dic in eventsArray) {
            EventsModel *event = [[EventsModel alloc] init];
            [event setValuesForKeysWithDictionary:dic];
            [self.array addObject:event];
            [event release];
            NSLog(@"-----%@", event.myowner.name);
        }
        
        //kvc
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

}
-(void)afnPostRequest
{
    NSString *str = @"http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=cee6710ae48a3945b398702d8702510a&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C552EE383-0FAD-4555-9979-AC38A01C5D6D%7C9C579DCC-7C8F-4E53-AEB6-54527C473309&format=json&loguid=&method=Recipe.getFindRecipe&nonce=1443856978&sessionid=1443856790&signmethod=md5&timestamp=1443856978&uuid=02288be08f4b871a69565746255b0de9&v=2&vc=40&vn=v5.1.0";
    
    
    //body体
    //    @"cacheKey=Recipe.getFindRecipe&sign=&uid=&uuid=02288be08f4b871a69565746255b0de9"
    
    NSDictionary *para = @{@"cacheKey":@"Recipe.getFindRecipe", @"uuid":@"02288be08f4b871a69565746255b0de9"};
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    
    AFHTTPRequestOperationManager *netManager = [AFHTTPRequestOperationManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [netManager POST:str parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"post请求成功====%@", dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息%@", error);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
